﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Transactions;

public partial class AddPhoto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string photoId = this.Request.QueryString["photoId"];
        if (photoId != null)
        {
            // show info of the image
            SqlConnection conn = new SqlConnection(
                ConfigurationManager.ConnectionStrings["PhotoDb"].ConnectionString);

            using (conn)
            {
                string query = "select name from photos where photoId = @PhotoId";
                SqlCommand command = new SqlCommand(query, conn);

                SqlParameter paramId = new SqlParameter("@PhotoId", photoId);
                command.Parameters.Add(paramId);

                conn.Open();
                string name = command.ExecuteScalar() as string;
                this.TextBoxName.Text = name;
            }
        }
    }

    protected void ButtonOk_Click(object sender, EventArgs e)
    {
        string photoId = this.Request.QueryString["photoId"];

        string directory = ConfigurationManager.AppSettings["repository"];
        if (directory == null || directory.Length == 0)
        {
            throw new ArgumentException("Please specifiy a repository location.");
        }
        if (!Directory.Exists(directory))
        {
            throw new DirectoryNotFoundException(directory);
        }

        string fileName = string.Format(
            @"{0}\{1}", directory, this.FileUploadSource.FileName);

        Stream stream = this.FileUploadSource.FileContent;

        SqlCommand command = null;
        if (photoId == null)
        {
            command = new SqlCommand("insert Photos (Name, Url) values(@Name, @Url)");
        }
        else
        {
            command = new SqlCommand(
                "update Photos set Name = @Name, Url = @Url where photoId = @PhotoId");

            SqlParameter paramId = new SqlParameter("@PhotoId", photoId);
            command.Parameters.Add(paramId);
        }
        SqlParameter paramName = new SqlParameter("@Name", this.TextBoxName.Text);
        SqlParameter paramUrl = new SqlParameter("@Url", fileName);

        command.Parameters.Add(paramName);
        command.Parameters.Add(paramUrl);

        this.Dump(fileName, command, stream);

        this.Response.Redirect("Default.aspx");
    }

    protected void Dump(string fileName, SqlCommand command, Stream stream)
    {
        if (fileName == null || fileName.Length == 0)
        {
            throw new ArgumentNullException("fileName", "Please suggest a source.");
        }
        if (command == null)
        {
            throw new ArgumentNullException("command", "Command cannot be null");
        }
        if (stream == null)
        {
            throw new ArgumentNullException("stream", "Stream cannot be null.");
        }

        SimpleFS.Services.Transactions.Implementations.SimpleResourceManager resourceManager
            = new SimpleFS.Services.Transactions.Implementations.SimpleResourceManager();

        SimpleFS.Storage.Windows.File file
            = new SimpleFS.Storage.Windows.File(fileName);

        SimpleFS.Services.Transactions.File txFile
            = new SimpleFS.Services.Transactions.File(file, resourceManager);

        using (TransactionScope scope = new TransactionScope())
        {
            using (SqlConnection conn = new SqlConnection(
                ConfigurationManager.ConnectionStrings["PhotoDb"].ConnectionString))
            {
                command.Connection = conn; conn.Open();
                command.ExecuteNonQuery();

                txFile.Write(stream);
                conn.Close();
            }
            scope.Complete();
        }
    }

    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("Default.aspx");
    }
}
